# coding:utf-8
'''
微博
https://m.weibo.cn/
https://m.weibo.cn/u/2803301701
'''
import urllib3
from urllib3 import PoolManager
import json
import pandas as pd

http = PoolManager()
urllib3.disable_warnings()

'''
人民日报
uid=2803301701
containerid=1076032803301701
新浪体育
uid=1638781994
containerid=1076031638781994
蔡徐坤
uid=1776448504
containerid=1076031776448504
共青团福建省委
uid=2730549204
containerid=1076032730549204
'''
uid = "1638781994"
blog_limit_count = 100

blog_items = []
page = 1
while True:
    url = "https://m.weibo.cn/api/container/getIndex?uid=%s&containerid=107603%s&page=%d" % (uid, uid, page)
    result = http.request(method="GET", url=url)
    json_data = json.loads(result.data.decode())
    if json_data["ok"] == 1:
        for card in json_data["data"]["cards"]:
            mblog = card.get("mblog", None)
            if mblog:
                blog_items.append([mblog["id"], mblog["created_at"], mblog["text"], mblog["attitudes_count"], mblog["comments_count"], mblog["reposts_count"]])
                print(mblog["text"])
                print("-"*10)
        if len(blog_items) > blog_limit_count:
            break
        else:
            page = page + 1
            continue
    else:
        break
blog_items = blog_items[:blog_limit_count]
df = pd.DataFrame(blog_items, columns=["ID", "发布时间", "内容(HTML格式)", "点赞数", "评论数", "转发数"])
df.to_excel("d:/weibo.xlsx")
